<?php
require_once ("BaseDAO.php");

// NewsItem Value Object
class NewsItem extends BaseVO {
	var $id;
	var $title;
	var $web_link;
	var $item;
	var $create_date;
	var $visibility;

	// create a new VO
	function NewsItem(
		$id= 0,
		$title= "",
		$web_link= "",
		$item= "",
		$create_date= "",
		$visibility= 0
	) {
		$this->id = $id;
		$this->title = $title;
		$this->web_link = $web_link;
		$this->item = $item;
		$this->create_date = $create_date;
		$this->visibility = $visibility;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->title == $vo->title &&
			$this->web_link == $vo->web_link &&
			$this->item == $vo->item &&
			$this->create_date == $vo->create_date &&
			$this->visibility == $vo->visibility;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->title = $vo->title;
		$this->web_link = $vo->web_link;
		$this->item = $vo->item;
		$this->create_date = $vo->create_date;
		$this->visibility = $vo->visibility;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->title . "," . $this->web_link . "," . $this->item . "," . $this->create_date . "," . $this->visibility;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<title>$this->title</title>\n" .
		"<web_link>$this->web_link</web_link>\n" .
		"<item>$this->item</item>\n" .
		"<create_date>$this->create_date</create_date>\n" .
		"<visibility>$this->visibility</visibility>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->title = $this->formHelper($prefix . "title", "");
		$this->web_link = $this->formHelper($prefix . "web_link", "");
		$this->item = $this->formHelper($prefix . "item", "");
		$this->create_date = $this->formHelper($prefix . "create_date", "");
		$this->visibility = $this->formHelper($prefix . "visibility", 0);
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->title = $this->queryHelper($prefix . "title", "");
		$this->web_link = $this->queryHelper($prefix . "web_link", "");
		$this->item = $this->queryHelper($prefix . "item", "");
		$this->create_date = $this->queryHelper($prefix . "create_date", "");
		$this->visibility = $this->queryHelper($prefix . "visibility", 0);
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// NewsItem Data Access Object
class NewsItemDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `news_item` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `news_item` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `news_item` (title,web_link,item,create_date,visibility) VALUES ('%A','%B','%C','%D',%E)";
	var $SQL_UPDATE = "UPDATE `news_item` SET ";
	var $SQL_DELETE = "DELETE FROM `news_item` WHERE id=%A";

	// default constructor
	function NewsItemDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single NewsItem obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new NewsItem(
				$row->id, 
				$row->title, 
				$row->web_link, 
				$row->item, 
				$row->create_date, 
				$row->visibility
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of NewsItem objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new NewsItem(
				$row->id, 
				$row->title, 
				$row->web_link, 
				$row->item, 
				$row->create_date, 
				$row->visibility
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of NewsItem objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new NewsItem(
				$row->id, 
				$row->title, 
				$row->web_link, 
				$row->item, 
				$row->create_date, 
				$row->visibility
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of NewsItem objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->title) > 0) $tmp .= "title like '%" . $this->safeEscapeStr($example->title) . "%' and ";
		if(strlen($example->web_link) > 0) $tmp .= "web_link like '%" . $this->safeEscapeStr($example->web_link) . "%' and ";
		if(strlen($example->item) > 0) $tmp .= "item like '%" . $this->safeEscapeStr($example->item) . "%' and ";
		if(strlen($example->create_date) > 0) $tmp .= "create_date = DATE('" . $this->safeEscapeStr($example->create_date) . "') and ";
		if($example->visibility != 0) $tmp .= "visibility = " . $this->safeEscapeStr($example->visibility) . " and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new NewsItem(
				$row->id, 
				$row->title, 
				$row->web_link, 
				$row->item, 
				$row->create_date, 
				$row->visibility
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->title), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->web_link), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->item), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->create_date), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->visibility), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "title = '" . $this->safeEscapeStr($vo->title) . "', ";
		$this->sql .= "web_link = '" . $this->safeEscapeStr($vo->web_link) . "', ";
		$this->sql .= "item = '" . $this->safeEscapeStr($vo->item) . "', ";
		$this->sql .= "create_date = '" . $this->safeEscapeStr($vo->create_date) . "', ";
		$this->sql .= "visibility = " . $this->safeEscapeStr($vo->visibility) . " ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

